package com.lxy.dataservice.service;

import com.lxy.dataservice.constants.MessageConstants;
import com.lxy.dataservice.dao.DurgTypeDao;
import com.lxy.dataservice.entity.DurgType;
import com.nip.db.mongodb.utils.tips.Response;
import com.nip.db.mongodb.utils.tips.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * DurgType
 *
 * @author < a href=" ">BBB</ a>
 * @version v1.0.01
 * @date 2019-12-26 16:06
 */
@Service
public class DurgTypeService {
  @Autowired
  DurgTypeDao durgTypeDao;

  /**
   * 查找所有药品分类
   */
  public List<DurgType> findAll() {
    return durgTypeDao.findAll();
  }

  /**
   * 根据id查药品分类
   */
  public DurgType findDurgTypeById(String id){
    return durgTypeDao.findById(id).orElse(new DurgType());
  }

  /**
   * 保存药品类名
   */
  public Response saveDurgType(DurgType entity){
    try {
      durgTypeDao.save(entity);
      return new Response(ResponseCode.CODE_200, MessageConstants.SUCCESS);
    }catch (Exception e){
      return new Response(ResponseCode.CODE_202,e.getMessage());
    }
  }

  /**
   * 删除药品分类
   */
  public Response deleteDurgType(String id){
    try {
      durgTypeDao.deleteById(id);
      return new Response(ResponseCode.CODE_200, MessageConstants.SUCCESS);
    } catch (Exception e) {
      return new Response(ResponseCode.CODE_202, e.getMessage());
    }
  }
}
